﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Telerik.Windows.Controls;
using DunSilverlight.Comm;
using DunLibrary.User;

namespace DunSilverlight.Views
{
    public partial class OperationLogPage : Page
    {
        private DunLibrary.Query.QueryCondition queryCondition;

        private const int Paging_Max_Result = 19;

        public OperationLogPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(OperationLog_Loaded);
            GetOpers();
            GetOpTypes();

        }

        void OperationLog_Loaded(object sender, RoutedEventArgs e)
        {
            BuildHql();
            QueryPaging(queryCondition);//
        }

        void GetOpers()
        {
            UserService.UserServiceClient ser = new UserService.UserServiceClient();
            ser.GetListByHQLCompleted += (object sender, UserService.GetListByHQLCompletedEventArgs e) =>
            {
                try
                {
                    int totalCount = 0;
                    IList<DunLibrary.User.User> users = JsonSerializerHelper.JsonToEntities<DunLibrary.User.User>(e.Result, out totalCount);

                    cmbOper.ItemsSource = users;
                    
                }
                catch (Exception ex)
                {
                    Tools.ShowMessage(ex.Message, "", false);
                }
            };
            //开始异步调用后台wcf服务,传入hql()
            ser.GetListByHQLAsync("from User");
        }
        void GetOpTypes() //没找到操作类型的定义 暂时没写！
        {
            cmbOpType.Items.Add("新增");
            cmbOpType.Items.Add("删除");
            cmbOpType.Items.Add("修改");
            cmbOpType.Items.Add("登陆");
            cmbOpType.Items.Add("退出");

        }
        private void BuildHql()
        {
            string hqlwhere =" where ol.OpType is not null";

            if (cmbOper.SelectedItem != null)
            {
                hqlwhere += " and ol.Oper.Id=" + (cmbOper.SelectedItem as DunLibrary.User.User).Id;
            }

            if(cmbOpType.SelectedItem != null)
            {
                hqlwhere += " and ol.OpType=" + "'" + cmbOpType.SelectedItem.ToString() + "'"; //没找到操作类型的定义 暂时没写！
            }
            if (txtBeginTime.SelectedValue != null && txtEndTime.SelectedValue==null)
            {
                hqlwhere += " and ol.OpTime>=" + "'" + txtBeginTime.SelectedValue.Value.ToString() + "'";
            }

            if (txtEndTime.SelectedValue != null && txtBeginTime.SelectedValue == null)
            {
                hqlwhere += " and ol.OpTime<=" + "'" + txtEndTime.SelectedValue.Value.ToString() + "'";
            }
            if (txtBeginTime.SelectedValue != null && txtEndTime.SelectedValue != null)
            {
                hqlwhere += " and ol.OpTime between " + "'"+ txtBeginTime.SelectedValue.Value.ToString()+"'" + " and " +"'"+txtEndTime.SelectedValue.Value.ToString()+"'";
            }

            
            queryCondition = new DunLibrary.Query.QueryCondition();

            queryCondition.HQL = "from OperationLog as ol " + hqlwhere;
            queryCondition.TotalHQL = "select count(*) from OperationLog as ol " + hqlwhere;

            queryCondition.MaxResults = Paging_Max_Result;
        }
        private void QueryPaging(DunLibrary.Query.QueryCondition qc)
        {
            if (qc == null || string.IsNullOrEmpty(qc.HQL))
                return;

            OperationLogService.OperationLogServiceClient ser = new OperationLogService.OperationLogServiceClient();

            ser.GetListByHQLWithPagingCompleted += (object sender, OperationLogService.GetListByHQLWithPagingCompletedEventArgs e) =>
            {
                try
                {
                    int totalCount = 0;

                    IList<OperationLog> operLogs = JsonSerializerHelper.JsonToEntities<OperationLog>(e.Result, out totalCount);

                    rDataPager.NumericButtonCount = totalCount / Paging_Max_Result;//分页好像不对

                    dgdLogs.ItemsSource = operLogs;
                    
                }
                catch (Exception ex)
                {
                    Tools.ShowMessage(ex.Message, "", false);
                }
                finally
                {
                    ser.CloseAsync();
                }
            };

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(qc);

            ser.GetListByHQLWithPagingAsync(json);
        }

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BuildHql();
            QueryPaging(queryCondition);//
        }

    }
}
